//
//  ZYComposeViewController.swift
//  WeiBo
//
//  Created by zhangyu on 16/10/8.
//  Copyright © 2016年 zhangyu. All rights reserved.
//

import UIKit

class ZYComposeViewController: UIViewController {

    private lazy var sendButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named:"common_button_orange"), for: .normal)
    
        button.setBackgroundImage(UIImage(named:"common_button_orange_highlighted"), for: .highlighted)
        
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), for: .disabled)
        button.setTitle("发送", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.gray, for: .disabled)
        button.size = CGSize(width: 45, height: 35)
        return button
    }()
    //标题
    private lazy var titleLabel: UILabel = {
        let label = UILabel(textColor: UIColor.darkGray, fontSize: 16)
        if let name = ZYUserAccountViewModel.sharedUserAccountViewModel.userAccount?.name {
            //通过富文本设置AttributedText属性
            let result = "发微博\n" + name
            //获取指定内容的范围
            let range = (result as NSString).range(of: name)
            //创建富文本
            let attributedStr: NSMutableAttributedString = NSMutableAttributedString(string: result)
            attributedStr.addAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 12), NSForegroundColorAttributeName:UIColor.brown], range: range)
            label.attributedText = attributedStr
        } else {
            label.text = "发微博"
        }
        //多行显示
        label.numberOfLines = 0
        label.textAlignment = .center
        label.sizeToFit()
        return label
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNavUI()
        view.backgroundColor = RandomColor()
    }
//    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//        dismiss(animated: true, completion: nil)
//    }
    private func setupNavUI() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", target: self, action: #selector(cancelAction))
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sendButton)
        navigationItem.rightBarButtonItem?.isEnabled = false
        navigationItem.titleView = titleLabel
    }
    @objc private func cancelAction() {
        dismiss(animated: true, completion: nil)
    }
}
